home *** CD-ROM | disk | FTP | other *** search
Text File | 1988-03-06 | 17.1 KB | 681 lines | [TEXT/MACH] |
- \ New Text Edit example
- \ J. Langowski for Mac Tutor March 1988
- \ derived from
- \ Editor Shell Example Program on Mach 2 demo disk
-
- \ found two 'features' of the new text edit while experimenting:
- \ a. when the insertion point is at a boundary between
- \ two different styles, the text typed will be TEKeyed
- \ according to the style BEFORE the insertion point, while
- \ TEGetStyle will return style information from AFTER the
- \ insertion point.
- \ b. Although the text face seems to be set inside the style record
- \ and properly associated with the text (TEGetStyle returns the correct
- \ information after the text face has been changed), the text is
- \ always drawn plain text style. The font and size changes work OK.
- \
-
- only forth definitions
- also assembler also mac
-
- \ ***** constants
-
- 300 CONSTANT APPLEID \ menu IDs for the menus to be set up
- 310 CONSTANT FILEID
- 320 CONSTANT EDITID
- 330 CONSTANT SIZEID
- 340 CONSTANT FontID
- 350 CONSTANT StyleID
-
- 20 CONSTANT InUpArrow \ part code for up arrow of scrollbar
- 21 CONSTANT InDownArrow \ part code for down arrow of scrollbar
- 22 CONSTANT InPageUp \ part code for page up region of scrollbar
- 23 CONSTANT InPageDown \ part code for page down region of scrollbar
- 129 CONSTANT InThumb \ part code for thumb of scrollbar
-
- $44525652 Constant "drvr
- $464F4E54 Constant "font
-
- %000001000000000 CONSTANT ShiftMask \ Mask used to isolate shift bit
- \ in modifiers word of event record
-
- $10 CONSTANT portRect \ Grafport rectangle
- $6E CONSTANT wVisible \ visible flag [byte]
-
- \ text edit equates
- 0 CONSTANT teDestRect ( destination rectangle [8 bytes] )
- 8 CONSTANT teViewRect ( view rectangle rectangle [8 bytes] )
- $C CONSTANT selRect ( select rectangle [8 bytes] )
- $18 CONSTANT teLineHite ( line height [word] )
- $1A CONSTANT teFontAscent \ font ascent [word]
- $1C CONSTANT teSelPoint \ selection point [long word]
- $20 CONSTANT teSelStart ( selection start [word] )
- $22 CONSTANT teSelEnd ( selection end [word] )
- $38 CONSTANT teCarOn ( is caret on? [byte] )
- $39 CONSTANT teCarAct ( is caret active? [byte] )
- $3C CONSTANT teLength ( length of text below [word] )
- $3E CONSTANT teTextH ( text [handle] )
- $48 CONSTANT teCROnly ( <CR> only for line breaks? [byte] )
- $4A CONSTANT teFont ( text font [word] )
- $4C CONSTANT teFace ( text face [word] )
- $4A CONSTANT teStylHandle \ handle to style record for new
- \ text edit. Never accessed directly
- $4E CONSTANT teMode \ text mode [word]
- $50 CONSTANT teSize \ font size [word] for old style record.
- \ for a new style record, this contains -1.
- \ in that case, teFont and teFace together contain
- \ the handle to the style record.
- $5E CONSTANT teNLines ( number of lines [word] )
- $60 CONSTANT teLines ( lines starts [word] )
-
- ( TextEdit Globals )
- $AB0 CONSTANT TEScrpLength ( textEdit Scrap Length [word] )
- $AB4 CONSTANT TEScrpHandle ( textEdit Scrap [handle] )
- $AF6 CONSTANT TEWdBreak ( default word break routine [pointer] )
-
- \ Event Record Equates
- $0 CONSTANT What ( event code [word] )
- $2 CONSTANT Message ( event message [long] )
- $6 CONSTANT When ( ticks since start-up [long] )
- $A CONSTANT Where ( mouse loc. pt. in global coords [long] )
- $E CONSTANT Modifiers ( modifier flags [word] )
-
- $0A CONSTANT LF ( ascii 'linefeed' )
- $20 CONSTANT SP ( ascii 'space' )
-
- create applestring 01 C, $14 C, \ Apple symbol
-
- \ ***** variables
-
- VARIABLE TEHandle ( handle for text edit record )
- VARIABLE TERect 4 VALLOT ( Text Edit view rectangle )
- VARIABLE SIZE ( item# of current textsize )
- VARIABLE DESKNAME 252 VALLOT \ holds name of desk accessory
- VARIABLE FONTNAME 252 VALLOT \ holds font name selected
- VARIABLE ITEMNAME 60 VALLOT \ receives menu item name
- VARIABLE MyStyle 8 VALLOT \ text style record for our private use
- \ fields of MyStyle:
- 0 CONSTANT tsFont
- 2 CONSTANT tsFace
- 4 CONSTANT tsSize
- 6 CONSTANT RGBColor
- VARIABLE currentFont \ font menu ID currently checked
- VARIABLE #fonts \ # of currently installed fonts
- VARIABLE currentSize \ size menu ID currently checked
-
- 76 USER AbortHook
- 152 USER ContentHook
- 160 USER GrowHook
- 164 USER CloseBoxHook
- 168 USER UpdateHook
- 172 USER ActivateHook
- 202 USER CAction \ holds address of control action routine
-
- \ ***** glue routines for new text edit
- \
- \ TEStylNew ( destRect viewRect -- TEHandle )
- \ is misspelled 'TEStyleNew' in the Mach2 trap definitions,
- \ but implemented. So are TEGetOffset and most of the other
- \ new text edit routines that are called through TEDispatch.
- \ One exception is TEStylInsert, which we are defining here:
-
- CODE TEStylInsert ( text length hST hTE -- )
- EXG D4,A7
- MOVE.L $C(A6),-(A7) \ pointer to text
- MOVE.L $8(A6),-(A7) \ length of text
- MOVE.L $4(A6),-(A7) \ style record handle
- MOVE.L (A6),-(A7) \ TE record handle
- ADDA.W #$10,A6
- MOVE.W #$7,-(A7)
- _TEDispatch
- EXG D4,A7
- RTS
- END-CODE
-
-
- NEW.WINDOW Editor
- " Editor" Editor TITLE
- 42 4 330 507 Editor BOUNDS
- DOCUMENT INVISIBLE CLOSEBOX GROWBOX Editor ITEMS
-
- 200 1000 TERMINAL EditTask
-
- NEW.MBAR EditBar
-
- NEW.MENU AppleMenu
- APPLESTRING AppleMenu TITLE
- 0 APPLEID AppleMenu BOUNDS
- " About Editor ...;(-" AppleMenu ITEMS
-
- NEW.MENU FileMenu
- " File" FileMenu TITLE
- 0 FileID FileMenu BOUNDS
- " New/N;Open.../O;Close;Save;Save as...;Revert to Original;(Print"
- FileMenu ITEMS
-
- NEW.MENU EditMenu
- " Edit" EditMenu TITLE
- 0 EDITID EditMenu BOUNDS
- " (Undo/Z;(-;Cut/K;Copy/C;Paste/V;Clear" EditMenu ITEMS
-
- NEW.MENU FontMenu
- " Font" FontMenu TITLE
- 0 FontID FontMenu BOUNDS
- " (Fonts<I; (-" FontMenu ITEMS
-
- NEW.MENU SizeMenu
- " Size" SizeMenu TITLE
- 0 SizeID SizeMenu BOUNDS
- " 9 Point; 10 Point; 12 Point; 14 Point; 18 Point; 20 Point; 24 Point"
- SizeMenu ITEMS
-
- CREATE SizeIDTable
- 0 , 0 , 0 c, \ no menu IDs for sizes 0 thru 8
- 1 c, 2 c, 0 c, 3 c, \ 9,10,--,12
- 0 c, 4 c, 0 c, 0 c, \ --,14,--,--
- 0 c, 5 c, 0 c, 6 c, \ --,18,--,20
- 0 c, 0 c, 0 c, 7 c, \ --,--,--,24
-
- CREATE SizeTable
- 0 c, 9 c, 10 c, 12 c, 14 c, 18 c, 20 c, 24 c,
-
-
- NEW.MENU StyleMenu
- " Style" StyleMenu TITLE
- 0 StyleID StyleMenu BOUNDS
- " Plain/P; Bold/B<B; Italic/I<I; Underline/U<U; Outline<O; Shadow<S; Condense; Extend"
- StyleMenu ITEMS
-
- NEW.CONTROL Scroll
- VSCROLLBAR VISIBLE 100 0 Scroll ITEMS
- VARIABLE lastVs
-
- NEW.CONTROL hScroll
- HSCROLLBAR VISIBLE 100 0 hScroll ITEMS
- VARIABLE lastHs
-
- : CHECK ( menuhandle item# flag -- ) ( checking a menu item )
- CALL CheckItem ;
-
- : =string { aStr bStr | -- flag }
- aStr count 65536 * bStr count rot + swap
- call CmpString 0=
- ;
-
- CODE @TEHandle ( -- handle ) ( an assembly language method of )
- MOVE.L TEHandle,-(A6) ( accessing a variable's contents )
- RTS
- END-CODE
-
- : Shift? ( - f ) \ checks the event record to see if the
- \ shift key was pressed.
- EVENT-RECORD Modifiers + W@ ( get modifiers word )
- ShiftMask AND ( isolate shiftbit )
- IF -1 ELSE 0 THEN
- ;
-
- : LineHeight ( - lineheight ) ( looks in the textedit record to )
- @TEHandle @ teLineHite + W@ ; ( see how tall each line is )
-
- : #Lines ( - #lines ) ( looks in the textedit record to )
- @TEHandle @ teNLines + W@ ; ( see how many lines of text there
- are in this file )
- : adjustFontMenu
- \ adjust font menu and currentFont variable
- myStyle w@ ( font ID ) fontName call getFname
- #fonts @ 0 DO
- fontMenu @ i itemName call GetItem
- itemName fontName =string
- IF FontMenu @ currentFont @ 0 check
- \ uncheck previous font selection
- FontMenu @ i -1 check
- i currentFont !
- leave
- THEN
- LOOP
- ;
-
- : adjustStyleMenu { | face - }
- myStyle tsFace + w@ -> face
- 8 0 DO
- 1 i scale face and ( get style bit )
- if -1 else 0 then
- styleMenu @ i 2+ rot check
- LOOP
- ;
-
- : adjustSizeMenu
- SizeMenu @ currentSize @ 0 check
- myStyle tsSize + w@ ( size )
- SizeIDTable + c@ ( sizeID )
- dup currentSize !
- SizeMenu @ swap -1 check
- ;
-
- : getCurrentStyle { | LHite FAsc -- }
- ( updates variable currentFont )
- ( size and Face kept in myStyle )
- ( LHite and FAsc are currently not used )
-
- @TEHandle @ teselStart + w@ \ get start of selection
- \ (or insertion point)
- ( offset ) myStyle ^ LHite ^ FAsc @TEHandle
- call TEGetStyle
-
- adjustFontMenu
- adjustStyleMenu
- adjustSizeMenu
- ;
-
- : AdjustTERect ( adjust terect size for the
- presence of scrollbars )
- portRect Editor + 4 + W@ ( get bottom coord )
- 16 - ( subtract 16 for height of scrollbar )
- teViewRect @TEHandle @ + 4 + W! ( store new coord back in text edit
- record )
-
- portRect Editor + 6 + W@ ( get right coord )
- 16 - ( subtract 16 for width of scrollbar )
- teViewRect @TEHandle @ + 6 + W! ( store new coord in textedit record )
- ;
-
- : Visible? ( - f ) ( checks visible flag in window )
- Editor wVisible + C@ ; ( record to see if window is
- currently visible )
-
- \ ***** event handlers *****
-
- : ACTIVATE-HANDLER
- RUN-ACTIVATE
- EVENT-RECORD Modifiers + W@ ( get modifiers word )
- 1 AND IF
- @TEHandle CALL TEActivate
- getCurrentStyle
- ELSE
- @TEHandle CALL TEDeactivate
- THEN
- ;
-
-
- : UPDATE-HANDLER
- Editor CALL SetPort
- AdjustTERect
- Editor CALL BeginUpdate
- Editor CALL DrawControls
- Editor CALL DrawGrowIcon
- Editor portRect + @TEHandle CALL TEUpdate
- Editor CALL EndUpdate
- ;
-
- : CONTENT-HANDLER { | theMouse -- }
- RUN-CONTENT
- Editor CALL SetPort
-
- ^ theMouse CALL GetMouse
- theMouse @TEHandle @ TEViewRect + call PtInRect
- IF
- theMouse Shift? @TEHandle CALL TEClick
- getCurrentStyle
- THEN
- ;
-
- : CLOSEBOX-HANDLER
- Editor ( Editor windowpointer )
- EVENT-RECORD Where + @ ( global mouse coordinates )
- CALL TrackGoAway ( follow the mouse to see if it
- is released in the closebox )
- IF Editor CALL HideWindow THEN
- ;
-
-
- \ **** main editor example code *****
-
- : POP-UP
- Editor CALL ShowWindow
- Editor CALL SelectWindow ( selecting the Editor window )
- EditBar @ CALL SetMenuBar ( make EditBar the current menubar )
- CALL DrawMenuBar ; ( redraw the menubar )
-
- : SetScrollLimits
- Scroll @ 0 CALL SetMinCtl
- Scroll @ #Lines CALL SetMaxCtl
- Scroll @ 0 CALL SetCtlValue
- 0 lastVs !
- hScroll @ 0 CALL SetMinCtl
- hScroll @ 1000 CALL SetMaxCtl
- hScroll @ 0 CALL SetCtlValue
- 0 lastHs !
- ;
-
-
- : EditFile { | char exitflag -- }
- BEGIN
- Visible?
- IF
- ?TERMINAL IF
- KEY -> char ( get the character )
- char 14 = IF
- 0 -> exitflag ( if cmd '.' exit )
- ELSE
- char @TEHandle CALL TEKey ( else insert )
- 1 -> exitflag ( char )
- THEN
- ELSE
- 1 -> exitflag ( if no key pressed, keep looping )
- THEN
-
- ELSE
- 0 -> exitflag ( if window's been closed, exit )
- THEN
- exitflag ( check exit condition )
- WHILE
- @TEHandle CALL TEIdle
- REPEAT
- ;
-
-
- : Open
- Pop-Up
- Editor CALL SetPort
-
- TERect TERect CALL TEStyleNew TEHandle ! \ get new style TE record
- -1 teCROnly @TEHANDLE @ + W! ( no word wrap )
- -1 teCarAct @TEHandle @ + C! ( activate caret )
- -1 @TEHandle call TEAutoView
-
- ( get the first 1K of text )
- 0 VIRTUAL 1024 0 @TEHandle TEStylinsert
- 0 0 @TEHandle CALL TESetSelect
- 15 ( doAll ) myStyle -1 ( redraw) @TEHandle
- call TESetStyle
- adjustFontMenu
- adjustStyleMenu
- adjustSizeMenu
-
- AdjustTERect ( initialize the text )
- PortRect Editor + @TEHandle CALL TEUpdate
- @TEHandle CALL TEDeactivate
- @TEHandle CALL TEActivate
-
- SetScrollLimits ( initialize the window's )
- Editor CALL DrawControls ( appearance )
- Editor CALL DrawGrowIcon
-
- ['] UPDATE-HANDLER UpdateHook ! ( install custom event )
- ['] CONTENT-HANDLER ContentHook ! ( handling routines )
- ['] ACTIVATE-HANDLER ActivateHook !
- ['] CLOSEBOX-HANDLER CloseBoxHook ! ;
-
-
- \ ***** menu handlers *****
-
- : HandleDeskAcc ( item# - )
- APPLEMENU @ SWAP DESKNAME CALL GETITEM
- DESKNAME CALL OPENDESKACC
- DROP
- ;
-
- : DO-APPLE ( item# - ) ( handles selections from the apple menu )
- dup 1 = ( check to see if it is the 'about' item )
- IF
- ( AboutEdit ) ( About Editor ... )
- drop
- ELSE ( otherwise, handle it as a desk accessory )
- HandleDeskAcc
- THEN
- ;
-
-
- : NewFile ; ( This is where the other menu items )
- : OpenFile ; ( would be handled )
- : CloseFile ;
- : SaveFile ;
- : SaveAs ;
- : Revert ;
-
- : DO-FILE ( item# - ) ( handles selections from the file menu )
- CASE
- 1 OF NewFile ENDOF
- 2 OF OpenFile ENDOF
- 3 OF CloseFile ENDOF
- 4 OF SaveFile ENDOF
- 5 OF SaveAs ENDOF
- 6 OF Revert ENDOF
- ENDCASE ;
-
- : DO-EDIT ( item# - ) ( handles selections from the edit menu )
- CASE
- 1 OF ( TEUndo ) ENDOF
- 3 OF @TEHandle CALL TECut ENDOF
- 4 OF @TEHandle CALL TECopy ENDOF
- 5 OF @TEHandle CALL TEStylPaste ENDOF
- 6 OF @TEHandle CALL TEDelete ENDOF
- ENDCASE ;
-
- : DO-Font { item# | fontID - } ( handles selections from the Font menu )
- FontMenu @ item# Fontname call getitem
- Fontname ^ fontID call getFNum
- ^ fontID w@ myStyle w!
- \ put into tsFont field of style record
- 1 ( doFont) myStyle -1 ( redraw) @TEHandle
- call TESetStyle
- FontMenu @ currentFont @ 0 check
- FontMenu @ item# -1 check
- item# currentFont !
- ;
-
- : Do-Style { item# | facefield -- }
- myStyle tsFace + -> facefield
- item# CASE
- 1 OF ( plain text )
- 0 facefield w!
- ENDOF
-
- facefield w@
- 1 item# 2- scale xor
- facefield w! \ flip bit
- ENDCASE
-
- 2 ( doFace) myStyle -1 ( redraw) @TEHandle
- call TESetStyle
- adjustStyleMenu
- ;
-
-
- : Do-Size ( item# - ) ( handles selections from the size menu )
- SizeTable + c@
- myStyle tsSize + w!
- 4 ( doSize) myStyle -1 ( redraw) @TEHandle
- call TESetStyle
- adjustSizeMenu
- ;
-
- : MBAR-HANDLER ( item# menuID - ) ( this word handles )
- CASE ( selections from the )
- APPLEID OF DO-APPLE ENDOF ( whole edit menubar )
- FILEID OF DO-FILE ENDOF
- EDITID OF DO-EDIT ENDOF
- FontID OF DO-Font ENDOF
- SIZEID OF DO-Size ENDOF
- STYLEID OF DO-Style ENDOF
- ENDCASE
- 0 CALL HILITEMENU
- ;
-
-
- \ ***** control action routines *****
-
- ( A control action routine specifies what action should take place
- WHILE a control is being held down.)
-
- : ScrollText { dv dh -- }
- dh dv @TEHandle CALL TEScroll
- ;
-
- : DO-Scroll { part-code | ctlvalue - }
- part-code
- CASE
- inuparrow OF Scroll @ CALL GetCtlValue -> ctlvalue
- ctlvalue 0= NOT
- IF
- Scroll @ ctlvalue 1- CALL SetCtlValue
- 5 0 ScrollText
- THEN
- ENDOF
-
- indownarrow OF Scroll @ CALL GetCtlValue -> ctlvalue
- ctlvalue #Lines = NOT
- IF
- Scroll @ ctlvalue 1+ CALL SetCtlValue
- -5 0 ScrollText
- THEN
- ENDOF
-
- inpageup OF Scroll @ CALL GetCtlValue -> ctlvalue
- ctlvalue 0= NOT
- IF
- Scroll @ ctlvalue 5 - CALL SetCtlValue
- 25 0 ScrollText
- THEN
- ENDOF
-
- inpagedown OF Scroll @ CALL GetCtlValue -> ctlvalue
- ctlvalue #Lines = NOT
- IF
- Scroll @ ctlvalue 5 + CALL SetCtlValue
- -25 0 ScrollText
- THEN
- ENDOF
- ENDCASE
- Scroll @ call GetCtlValue lastVs !
- ;
-
- : DO-hScroll { part-code | ctlvalue - }
- part-code
- CASE
- inuparrow OF hScroll @ CALL GetCtlValue -> ctlvalue
- ctlvalue 0= NOT
- IF
- hScroll @ ctlvalue 1- CALL SetCtlValue
- 0 5 ScrollText
- THEN
- ENDOF
-
- indownarrow OF hScroll @ CALL GetCtlValue -> ctlvalue
- ctlvalue #Lines = NOT
- IF
- hScroll @ ctlvalue 1+ CALL SetCtlValue
- 0 -5 ScrollText
- THEN
- ENDOF
-
- inpageup OF hScroll @ CALL GetCtlValue -> ctlvalue
- ctlvalue 0= NOT
- IF
- hScroll @ ctlvalue 5 - CALL SetCtlValue
- 0 25 ScrollText
- THEN
- ENDOF
-
- inpagedown OF hScroll @ CALL GetCtlValue -> ctlvalue
- ctlvalue #Lines = NOT
- IF
- hScroll @ ctlvalue 5 + CALL SetCtlValue
- 0 -25 ScrollText
- THEN
- ENDOF
- ENDCASE
- hScroll @ call GetCtlValue lastHs !
- ;
-
- : ControlAction ( part-code control-handle - )
- CASE
- Scroll @ OF DO-Scroll ENDOF
- hScroll @ OF DO-hScroll ENDOF
- swap drop
- ENDCASE
- ;
-
-
- \ ***** scrollbar thumb control handler *****
-
- : DO-vThumb { | ctlV }
- inThumb = IF
- scroll @ call getCtlValue -> ctlV
- lastVs @ ctlV - 5 * 0 scrollText
- ctlV lastVs !
- THEN
- ;
-
- : DO-hThumb { | ctlV }
- inThumb = IF
- hscroll @ call getCtlValue -> ctlV
- 0 lastHs @ ctlV - 5 * scrollText
- ctlV lastHs !
- THEN
- ;
-
- : ControlHandler ( part-code control-handle - )
- CASE
- Scroll @ OF DO-vThumb ENDOF
- hScroll @ OF DO-hThumb ENDOF
- swap drop
- ENDCASE
- ;
-
-
-
- \ ***** initialization *****
-
- : INIT-MBAR
- EditBar ADD
- EditBar APPLEMENU ADD
- APPLEMENU @ "drvr call addresmenu
- EditBar FileMenu ADD
- EditBar EditMenu ADD
- EditBar FontMenu ADD
- Fontmenu @ "font call addresmenu
- Fontmenu @ call countMItems #fonts !
- EditBar SizeMenu ADD
- EditBar StyleMenu ADD
- ;
-
- : INIT-TASK
- Editor ADD ( make the Editor window )
- Editor Scroll ADD ( add vertical scroll bar )
- Editor hScroll ADD ( add horizontal scroll bar )
- Editor EditTask BUILD ; ( link the window to the task )
-
- : START-TASK
- ACTIVATE
- ['] ControlAction CAction !
- ['] ControlHandler Control-Vector !
- ['] MBAR-HANDLER MENU-VECTOR ! ( install menu handling routine )
- BEGIN
- STANDARD-GETFILE
- IF
- Open
- EditFile
- THEN
- bye
- AGAIN
- ;
-
- : INIT-EDIT ( initializes and starts Editor )
- INIT-TASK ( make the task and window )
- INIT-MBAR ( make the menubar and the menus )
- EditBar EditTask MBAR>TASK ( link the menubar to the task )
- 4 myStyle w! \ default font, Monaco
- 0 myStyle 2+ w! \ default face, plain text
- 9 myStyle 4 + w! \ default size, 9 point
- 0 myStyle 6 + ! \ RGBcolor = ...
- 0 myStyle 10 + w! \ ...black
- 4 TERect W! ( define the text edit rectangle )
- 4 TERect 2+ W!
- 288 TERect 4 + W!
- 503 TERect 6 + W!
- EditTask START-TASK
- ;
-
- cr .( to create a stand-alone application, type: )
- cr .( TURNKEY INIT-EDIT editor )